package com.aw.puzzle.dropboxdiet;


import java.util.List;

/**
 * User: gmc
 * Date: 29/01/11
 */
public class CombinationSetManager {
    private List<Activity> activities;
    private int currentIdCmbSet = 0;
    private int activitiesXGroup;
    private boolean neededToContinueSearching;
    private CombinationSet firstCombSetGreater;
    private CombinationSet lastCombinationSet;
    private CombinationSet combSetInitial;

    public CombinationSetManager(int activitiesXGroup, List<Activity> activities) {
        this.activitiesXGroup = activitiesXGroup;
        this.activities = activities;
    }

    public void setCombSetInitial(CombinationSet combSetInitial) {
        this.combSetInitial = combSetInitial;
        if (this.combSetInitial != null) {
            activitiesXGroup = combSetInitial.getActivitiesXGroup();
        }
    }

    public ActivityGroup searchGroupWithTheSameValueOf(ActivityGroup groupToSearch) {
        neededToContinueSearching = true;
        CombinationSet combSet = getFirstCombinationSet();
        while (combSet != null) {
            ActivityGroup group = combSet.getFirstGroup();
            if (group == null) {
                return null;
            }
            while (group != null) {
                if (group.getValue() == groupToSearch.getValue()) {
                    return group;
                } else if (group.getValue() > groupToSearch.getValue()) {
                    if (firstCombSetGreater == null) {
                        firstCombSetGreater = combSet;
                    }
                    if (combSet.isFirst() && combSet.getNumberOfGeneratedGroups() == 1) {
                        neededToContinueSearching = false;
                        return null;
                    } else {
                        break;
                    }
                }
                group = combSet.getNextGroup();
            }
            combSet = getNextCombinationSet();
        }
        return null;
    }

    private CombinationSet getFirstCombinationSet() {
        CombinationSet firstCombinationSet;
        if (combSetInitial != null) {
            firstCombinationSet = combSetInitial;
            firstCombinationSet.setAtFirstReturnLastCombination();
            currentIdCmbSet = combSetInitial.getId();
        } else {
            firstCombinationSet = new CombinationSet(currentIdCmbSet, activities, activitiesXGroup);
        }
        setLastCombinationSet(firstCombinationSet);
        return firstCombinationSet;
    }

    public CombinationSet getNextCombinationSet() {
        CombinationSet newCombinationSet = new CombinationSet(currentIdCmbSet, activities, activitiesXGroup);
        newCombinationSet.setPreviousCombinationSet(lastCombinationSet);
        setLastCombinationSet(newCombinationSet);
        return newCombinationSet;
    }

    private void setLastCombinationSet(CombinationSet combinationSet) {
        lastCombinationSet = combinationSet;
        currentIdCmbSet++;
    }

    public boolean isNeededToContinueSearching() {
        return neededToContinueSearching;
    }

    public CombinationSet getFirstCombSetGreater() {
        return firstCombSetGreater;
    }
}
